﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using CMP.ERP.BLL;
using CMP.ERP.Model;

namespace CMP.ERP.Admin.Controllers
{
    public class ShopController : Controller
    {
        //
        // GET: /Shop/
        UsersBLL bll = new UsersBLL();
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public ActionResult Shop_Show()
        {
            List<Users> list = new List<Users>();
            DataTable data = bll.ShowUsers();
            foreach (DataRow item in data.Rows)
            {
                Users e = new Users();
                e.UName = item["UName"].ToString();
                e.UPwd = item["UPwd"].ToString();
                e.CompanyName = item["CompanyName"].ToString();
                e.Ustate = item["Ustate"].ToString();
                e.UIds = Convert.ToInt32(item["UIds"]);
                list.Add(e);
            }
            return View(list);
        }
        //根据名称查询用户信息
        public ActionResult SelUsers(string UName)
        {
            List<Users> list = new List<Users>();
            DataTable data = bll.SelUsers(UName);
            foreach (DataRow item in data.Rows)
            {
                Users e = new Users();
                e.UName = item["UName"].ToString();
                e.UPwd = item["UPwd"].ToString();
                e.CompanyName = item["CompanyName"].ToString();
                e.Ustate = item["Ustate"].ToString();
                e.UIds = Convert.ToInt32(item["UIds"]);
                list.Add(e);
            }
            return View("Shop_Show", list);
        }
        //删除
        public ActionResult DelUsers(int id)
        {
            if(bll.DelUsers(id)>0)
            {
                return Content("<script>alert('删除成功!');location.href='/Shop/Shop_Show'</script>");
            }
            else
            {
                return Content("<script>alert('删除失败!');location.href='/Shop/Shop_Show'</script>");
            }
        }
        public ActionResult StateUsers(int id)
        {
            Users e = new Users();
            DataTable data = bll.GetUsers(id);
            foreach (DataRow item in data.Rows)
            {
                e.UName = item["UName"].ToString();
                e.UPwd = item["UPwd"].ToString();
                e.CompanyName = item["CompanyName"].ToString();
                e.Ustate = item["Ustate"].ToString();
                e.UIds = Convert.ToInt32(item["UIds"]);
            }
            if(e.Ustate=="正常")
            {
                Users u = new Users();
                u.Ustate = "异常";
                u.UIds = e.UIds;
                bll.StateUsers(u);
                return Content("<script>location.href='/Shop/Shop_Show'</script>");
            }
            else
            {
                Users u = new Users();
                u.Ustate = "正常";
                u.UIds = e.UIds;
                bll.StateUsers(u);
                return Content("<script>location.href='/Shop/Shop_Show'</script>");
            }
        }
        //修改
        public ActionResult UpdUsers(int id)
        {
            Users e = new Users();
            DataTable data = bll.GetUsers(id);
            foreach (DataRow item in data.Rows)
            {
                e.UName = item["UName"].ToString();
                e.UPwd = item["UPwd"].ToString();
                e.CompanyName = item["CompanyName"].ToString();
                e.Ustate = item["Ustate"].ToString();
                e.UIds = Convert.ToInt32(item["UIds"]);
            }
            return View(e);
        }
        [HttpPost]
        public ActionResult UpdUsers(Users u)
        {
            if (bll.UpdUsers(u) > 0)
            {
                return Content("<script>alert('修改成功!');location.href='/Shop/Shop_Show'</script>");
            }
            else
            {
                return Content("<script>alert('修改失败!');location.href='/Shop/UpdUsers'</script>");
            }
        }
    }
}
